package com.lanyun.aicorrectassistant.controller.admin.authUser;

import com.google.common.base.Preconditions;
import com.lanyun.aicorrectassistant.controller.admin.authUser.vo.AuthUserLoginResponseVO;
import com.lanyun.aicorrectassistant.controller.admin.authUser.vo.AuthUserRequestPageVO;
import com.lanyun.aicorrectassistant.controller.admin.authUser.vo.AuthUserRequestVO;
import com.lanyun.aicorrectassistant.controller.admin.authUser.vo.AuthUserResponseVO;
import com.lanyun.aicorrectassistant.domain.authUser.AuthUserDomainService;
import com.lanyun.aicorrectassistant.basic.bean.PageResult;
import com.lanyun.aicorrectassistant.basic.bean.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;

import java.util.HashMap;

/**
 * 用户表控制器
 *
 * @author makejava
 * @version 1.0
 * @date 2025-05-12 01:53:15
 */
@RequestMapping("/api-admin/authUser")
@RestController("adminAuthUserController")
@Slf4j
public class AuthUserController {

    @Resource
    private AuthUserDomainService authUserDomainService;

    /**
     * 登录
     */
    @PostMapping("/doLogin")
    public Result<AuthUserLoginResponseVO> doLogin(@RequestBody AuthUserRequestVO authUserRequestVO) {
        try {
            Preconditions.checkNotNull(authUserRequestVO.getUsername(), "用户名不能为空");
            Preconditions.checkNotNull(authUserRequestVO.getPassword(), "密码不能为空");

            AuthUserLoginResponseVO authUserLoginResponseVO = authUserDomainService.doLogin(authUserRequestVO);
            return Result.success(authUserLoginResponseVO);
        } catch (IllegalArgumentException iae) {
            log.error("AuthUserController.doLogin -> 管理员登录异常，authUserRequestVO:{}", authUserRequestVO, iae);
            return Result.fail(HttpStatus.SC_BAD_REQUEST, iae.getMessage());
        }
    }


    /**
     * 分页查询用户表
     */
    @PostMapping("/page")
    public Result<PageResult<AuthUserResponseVO>> page(@RequestBody AuthUserRequestPageVO requestPageVO) {
        return Result.success(authUserDomainService.page(requestPageVO));
    }

    /**
     * 新增用户表
     */
    @PostMapping("/create")
    public Result<Boolean> create(@RequestBody AuthUserRequestVO requestVO) {
        return Result.success(authUserDomainService.insert(requestVO));
    }

    /**
     * 修改用户表
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody AuthUserRequestVO requestVO) {
        return Result.success(authUserDomainService.update(requestVO));
    }

    /**
     * 删除用户表
     */
    @DeleteMapping("/delete/{id}")
    public Result<Boolean> delete(@PathVariable("id") Long id) {
        return Result.success(authUserDomainService.delete(id));
    }

    /**
     * 获取用户表详情
     */
    @GetMapping("/get/{id}")
    public Result<AuthUserResponseVO> get(@PathVariable("id") Long id) {
        return Result.success(authUserDomainService.get(id));
    }
}
